package com.irdstudio.efp.cus.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gdrcu.efp.cus.common.CusConstant;
import com.irdstudio.efp.cus.service.dao.CusHandoverAppDao;
import com.irdstudio.efp.cus.service.dao.CusHandoverLstDao;
import com.irdstudio.efp.cus.service.domain.CusHandoverApp;
import com.irdstudio.efp.cus.service.domain.CusHandoverLst;
import com.irdstudio.efp.cus.service.facade.CusHandoverAppService;
import com.irdstudio.efp.cus.service.vo.CusHandoverAppVO;
import com.irdstudio.efp.cus.service.vo.CusHandoverLstVO;
import com.irdstudio.basic.framework.core.base.AbstractFrameworkService;
import com.irdstudio.basic.framework.core.base.FrameworkService;
import com.irdstudio.basic.framework.core.constant.SequenceConstance;
import com.irdstudio.basic.framework.core.vo.CusHandOverVO;

/**
 * <p>
 * ServiceImpl: 客户移交申请
 * <p>
 * 
 * @author chenqm1
 * @date 2018-05-14
 */
@Service("cusHandoverAppService")
public class CusHandoverAppServiceImpl extends AbstractFrameworkService implements CusHandoverAppService, FrameworkService {

	private static Logger logger = LoggerFactory.getLogger(CusHandoverAppServiceImpl.class);

	@Autowired
	private CusHandoverAppDao cusHandoverAppDao;
	@Autowired
	private CusHandoverLstDao cusHandoverLstDao;

	/**
	 * 新增操作
	 */
	@Override
	public int insertCusHandoverApp(CusHandoverAppVO inCusHandoverAppVo) {
		logger.debug("当前新增数据为:" + inCusHandoverAppVo.toString());
		int num = 0;
		try {
			CusHandoverApp cusHandoverApp = new CusHandoverApp();
			inCusHandoverAppVo.setStatus(CusConstant.STD_ZB_HAND_STATUS_00);
			beanCopy(inCusHandoverAppVo, cusHandoverApp);
			num = cusHandoverAppDao.insertCusHandoverApp(cusHandoverApp);
		} catch (Exception e) {
			logger.error("新增数据发生异常!", e);
			num = -1;
		}
		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	/**
	 * 删除操作
	 */
	@Override
	public int deleteByPk(CusHandoverAppVO inCusHandoverAppVo) {
		logger.debug("当前删除数据条件为:" + inCusHandoverAppVo);
		int num = 0;
		try {
			CusHandoverApp cusHandoverApp = new CusHandoverApp();
			beanCopy(inCusHandoverAppVo, cusHandoverApp);
			num = cusHandoverAppDao.deleteByPk(cusHandoverApp);
			if (num > 0) {
				num += cusHandoverLstDao.deleteByApplySerno(cusHandoverApp.getSerno());
			}
		} catch (Exception e) {
			logger.error("删除数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inCusHandoverAppVo + "删除的数据条数为" + num);
		return num;
	}

	/**
	 * 更新操作
	 */
	@Override
	public int updateByPk(CusHandoverAppVO inCusHandoverAppVo) {
		logger.debug("当前修改数据为:" + inCusHandoverAppVo.toString());
		int num = 0;
		try {
			CusHandoverApp cusHandoverApp = new CusHandoverApp();
			String openday = this.getOpenday();
			inCusHandoverAppVo.setSuperviseDate(openday); // 审批日期
			beanCopy(inCusHandoverAppVo, cusHandoverApp);
			num = cusHandoverAppDao.updateByPk(cusHandoverApp);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + inCusHandoverAppVo + "修改的数据条数为" + num);
		return num;
	}

	/**
	 * 查询操作
	 */
	@Override
	public CusHandoverAppVO queryByPk(CusHandoverAppVO inCusHandoverAppVo) {

		logger.debug("当前查询参数信息为:" + inCusHandoverAppVo);
		try {
			CusHandoverApp queryCusHandoverApp = new CusHandoverApp();
			beanCopy(inCusHandoverAppVo, queryCusHandoverApp);
			CusHandoverApp queryRslCusHandoverApp = cusHandoverAppDao.queryByPk(queryCusHandoverApp);
			if (Objects.nonNull(queryRslCusHandoverApp)) {
				CusHandoverAppVO outCusHandoverAppVo = beanCopy(queryRslCusHandoverApp, new CusHandoverAppVO());
				logger.debug("当前查询结果为:" + outCusHandoverAppVo.toString());
				return outCusHandoverAppVo;
			} else {
				logger.debug("当前查询结果为空!");
			}
		} catch (Exception e) {
			logger.error("查询数据发生异常!", e);
		}
		return null;
	}

	/**
	 * 查询用户权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<CusHandoverAppVO> queryAllOwner(CusHandoverAppVO cusHandoverAppVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<CusHandoverAppVO> list = null;
		try {
			List<CusHandoverApp> cusHandoverApps = cusHandoverAppDao.queryAllOwnerByPage(cusHandoverAppVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + cusHandoverApps.size());
			pageSet(cusHandoverApps, cusHandoverAppVo);
			list = (List<CusHandoverAppVO>) beansCopy(cusHandoverApps, CusHandoverAppVO.class);

		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}
		return list;

	}

	/**
	 * 查询当前机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<CusHandoverAppVO> queryAllCurrOrg(CusHandoverAppVO cusHandoverAppVo) {
		logger.debug("当前查询本人所属机构数据信息的参数信息为:");
		List<CusHandoverApp> cusHandoverApps = cusHandoverAppDao.queryAllCurrOrgByPage(cusHandoverAppVo);
		logger.debug("当前查询本人所属机构数据信息的结果集数量为:" + cusHandoverApps.size());
		List<CusHandoverAppVO> list = null;
		try {
			pageSet(cusHandoverApps, cusHandoverAppVo);
			list = (List<CusHandoverAppVO>) beansCopy(cusHandoverApps, CusHandoverAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	/**
	 * 查询当前机构及下属机构权限数据
	 */
	@Override
	@SuppressWarnings("unchecked")
	public List<CusHandoverAppVO> queryAllCurrDownOrg(CusHandoverAppVO cusHandoverAppVo) {
		logger.debug("当前查询本人所属机构及以下数据信息的参数信息为:");
		List<CusHandoverApp> cusHandoverApps = cusHandoverAppDao.queryAllCurrDownOrgByPage(cusHandoverAppVo);

		logger.debug("当前查询本人所属机构及以下数据信息的结果集数量为:" + cusHandoverApps.size());
		List<CusHandoverAppVO> list = null;
		try {
			pageSet(cusHandoverApps, cusHandoverAppVo);
			list = (List<CusHandoverAppVO>) beansCopy(cusHandoverApps, CusHandoverAppVO.class);
		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;

	}

	@SuppressWarnings("unchecked")
	@Override
	public int insertCusHandoverAppAndDetailLst(CusHandoverAppVO inCusHandoverAppVo, List<CusHandoverLstVO> cusHandoverLstVOList) {
		if (inCusHandoverAppVo == null || cusHandoverLstVOList == null || cusHandoverLstVOList.size() == 0)
			return -1;
		logger.debug("当前新增数据为:" + inCusHandoverAppVo.toString());
		int num = 0;

		String openday = this.getOpenday(); // 营业日期
		CusHandoverApp cusHandoverApp = new CusHandoverApp();
		String serno = null;
		try {
			serno = this.getSequence(SequenceConstance.CUS_HANDOVER_APP_SERNO);
		} catch (Exception e) {
			logger.error("生成流水号发生异常", e);
			num = -1;
			return num;
		}
		inCusHandoverAppVo.setInputDate(openday);
		inCusHandoverAppVo.setStatus(CusConstant.STD_ZB_HAND_STATUS_00);
		beanCopy(inCusHandoverAppVo, cusHandoverApp);
		cusHandoverApp.setSerno(serno);
		num = cusHandoverAppDao.insertCusHandoverApp(cusHandoverApp);
		if (num > 0) {
			for (int i = 0; i < cusHandoverLstVOList.size(); i++) {
				cusHandoverLstVOList.get(i).setSerno(serno);
			}
			List<CusHandoverLst> cusHandoverLst = null;
			try {
				cusHandoverLst = (List<CusHandoverLst>) beansCopy(cusHandoverLstVOList, CusHandoverLstVO.class);
			} catch (Exception e) {
				logger.error("列表转换发生异常", e);
				num = -1;
			}
			num += cusHandoverLstDao.insertCusHandoverLst(cusHandoverLst);
		}

		logger.debug("当前新增数据条数为:" + num);
		return num;
	}

	@Override
	public int queryHandoverCusManager(CusHandoverAppVO inCusHandoverAppVo) {
		// 移交未结束的 status in ('00','10','20') , 移交范围为 按客户经理
		inCusHandoverAppVo.setHandoverScope(CusConstant.HANDOVER_MODE_BY_CUS_MANAGER);
		int num = cusHandoverAppDao.queryCusHandoverApplyNotEnd(inCusHandoverAppVo);
		return num;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CusHandoverAppVO> queryApprovalAllOwner(CusHandoverAppVO cusHandoverAppVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<CusHandoverAppVO> list = null;
		try {
			List<CusHandoverApp> cusHandoverApps = cusHandoverAppDao.queryApprovalAllOwnerByPage(cusHandoverAppVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + cusHandoverApps.size());
			pageSet(cusHandoverApps, cusHandoverAppVo);
			list = (List<CusHandoverAppVO>) beansCopy(cusHandoverApps, CusHandoverAppVO.class);

		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CusHandoverAppVO> queryApprovalAllCurrOrg(CusHandoverAppVO cusHandoverAppVo) {

		logger.debug("当前查询本人所在机构所属数据信息的参数信息为:");
		List<CusHandoverAppVO> list = null;
		try {
			List<CusHandoverApp> cusHandoverApps = cusHandoverAppDao.queryApprovalAllCurrOrgByPage(cusHandoverAppVo);
			logger.debug("当前查询本人所在机构所属数据信息的结果集数量为:" + cusHandoverApps.size());
			pageSet(cusHandoverApps, cusHandoverAppVo);
			list = (List<CusHandoverAppVO>) beansCopy(cusHandoverApps, CusHandoverAppVO.class);

		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CusHandoverAppVO> queryApprovalAllCurrDownOrg(CusHandoverAppVO cusHandoverAppVo) {

		logger.debug("当前查询本人所在机构及下属机构所属数据信息的参数信息为:");
		List<CusHandoverAppVO> list = null;
		try {
			List<CusHandoverApp> cusHandoverApps = cusHandoverAppDao.queryApprovalAllCurrDownOrgByPage(cusHandoverAppVo);
			logger.debug("当前查询本人所在机构及下属机构所属数据信息的结果集数量为:" + cusHandoverApps.size());
			pageSet(cusHandoverApps, cusHandoverAppVo);
			list = (List<CusHandoverAppVO>) beansCopy(cusHandoverApps, CusHandoverAppVO.class);

		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CusHandoverAppVO> queryRecvAllOwner(CusHandoverAppVO cusHandoverAppVo) {

		logger.debug("当前查询本人所属数据信息的参数信息为:");
		List<CusHandoverAppVO> list = null;
		try {
			List<CusHandoverApp> cusHandoverApps = cusHandoverAppDao.queryRecvAllOwnerByPage(cusHandoverAppVo);
			logger.debug("当前查询本人所属数据信息的结果集数量为:" + cusHandoverApps.size());
			pageSet(cusHandoverApps, cusHandoverAppVo);
			list = (List<CusHandoverAppVO>) beansCopy(cusHandoverApps, CusHandoverAppVO.class);

		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CusHandoverAppVO> queryRecvAllCurrOrg(CusHandoverAppVO cusHandoverAppVo) {

		logger.debug("当前查询本人所在机构所属数据信息的参数信息为:");
		List<CusHandoverAppVO> list = null;
		try {
			List<CusHandoverApp> cusHandoverApps = cusHandoverAppDao.queryRecvAllCurrOrgByPage(cusHandoverAppVo);
			logger.debug("当前查询本人所在机构所属数据信息的结果集数量为:" + cusHandoverApps.size());
			pageSet(cusHandoverApps, cusHandoverAppVo);
			list = (List<CusHandoverAppVO>) beansCopy(cusHandoverApps, CusHandoverAppVO.class);

		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CusHandoverAppVO> queryRecvAllCurrDownOrg(CusHandoverAppVO cusHandoverAppVo) {

		logger.debug("当前查询本人所在机构及下属机构所属数据信息的参数信息为:");
		List<CusHandoverAppVO> list = null;
		try {
			List<CusHandoverApp> cusHandoverApps = cusHandoverAppDao.queryRecvAllCurrDownOrgByPage(cusHandoverAppVo);
			logger.debug("当前查询本人所在机构及下属机构所属数据信息的结果集数量为:" + cusHandoverApps.size());
			pageSet(cusHandoverApps, cusHandoverAppVo);
			list = (List<CusHandoverAppVO>) beansCopy(cusHandoverApps, CusHandoverAppVO.class);

		} catch (Exception e) {
			logger.error("数据转换出现异常!", e);
		}

		return list;
	}

	@Override
	public int updateStatusBySernoCondition(CusHandoverAppVO cusHandoverAppVo) {
		logger.debug("当前修改数据为:" + cusHandoverAppVo);
		int num = 0;
		try {
			num = cusHandoverAppDao.updateStatusBySernoCondition(cusHandoverAppVo);
		} catch (Exception e) {
			logger.error("修改数据发生异常!", e);
			num = -1;
		}
		logger.debug("根据条件:" + cusHandoverAppVo + "修改的数据条数为" + num);
		return num;
	}

	@Override
	public int autoUpdateHandoverCusBusiness() throws Exception {
		int allNum = 0; // 当前跑批更新的总数
		logger.info("日终跑批移交接收成功的客户业务资料信息开始");
		// 查询当前客户移交[成功],但未[移交跑批成功] 的客户移交申请信息
		List<CusHandoverApp> applys = cusHandoverAppDao.queryCusHandoverRescAll();
		if (applys != null && applys.size() > 0) {
			try {
				CusHandOverVO cusHandOverVO;
				List<String> cusIds;
				CusHandoverAppVO inVO;
				int num = 0; // 每次 客户移交申请 更新的总数

				int updateNum = 0;
				String serno;
				for (CusHandoverApp apply : applys) {
					serno = apply.getSerno();
					num = 0;
					cusHandOverVO = new CusHandOverVO();
					// 只有客户移交方式 [客户资料与业务移交] 和 [一切资料与业务移交], 才更新下面的信息
					if (CusConstant.CUS_HANDOVERAPP_MODE_2.equals(apply.getHandoverMode())
							|| CusConstant.CUS_HANDOVERAPP_MODE_3.equals(apply.getHandoverMode())) {

						cusIds = cusHandoverLstDao.queryCusIdsBySerno(apply.getSerno());// 客户号列表

						cusHandOverVO.setCusIds(cusIds);
						cusHandOverVO.setFromUser(apply.getHandoverId());// 移出用户（客户经理）
						cusHandOverVO.setToUser(apply.getReceiverId());// 接收用户（客户经理）
						cusHandOverVO.setFromBrId(apply.getHandoverBrId());// 移出机构（管理机构）
						cusHandOverVO.setToBrId(apply.getReceiverBrId());// 接收机构（管理机构）

						updateNum = 0;// pvpCusHandOverService.doCusHandOver(cusHandOverVO);// 贷后检查登记表客户移交
						num += updateNum;
						logger.info(serno + "贷后检查登记表客户移交更新的总数:" + updateNum);

						updateNum = 0;// ctrCusHandOverService.doCusHandOver(cusHandOverVO);// 借款合同表移交
						num += updateNum;
						logger.info(serno + "借款合同表移交更新的总数:" + updateNum);

						updateNum = 0;// lmtCusHandOverService.doCusHandOver(cusHandOverVO); // 授信申请表移交\授信协议表移交\预授信名单表移交\额度冻结表记录 \额度解冻表记录
						num += updateNum;
						logger.info(serno + "授信申请表移交\\授信协议表移交\\预授信名单表移交\\额度冻结表记录 \\额度解冻表记录移交更新的总数:" + updateNum);

						updateNum = 0;// loanCusHandOverService.doCusHandOver(cusHandOverVO); // 贷款台账表移交\贷款台账子表移交
						num += updateNum;
						logger.info(serno + "贷款台账表移交\\贷款台账子表移交更新的总数:" + updateNum);

						updateNum = 0;// rscCusHandOverService.doCusHandOver(cusHandOverVO); // 风险分类表移交\风险分类借据表移交\风险分类合同表移交\风险分类解除借据表移交
						num += updateNum;
						logger.info(serno + "风险分类表移交\\风险分类借据表移交\\风险分类合同表移交\\风险分类解除借据表移交更新的总数:" + updateNum);

						updateNum = 0;// pspCusHandOverService.doCusHandOver(cusHandOverVO);// 贷后检查登记表移交
						num += updateNum;
						logger.info(serno + "贷后检查登记表移交更新的总数:" + updateNum);
					}

					inVO = new CusHandoverAppVO();
					inVO.setSerno(apply.getSerno());
					inVO.setStatus(CusConstant.STD_ZB_HAND_STATUS_50); // 移交跑批成功
					List<String> needStatus = new ArrayList<>(); // 只有 [成功] 状态才能更新
					needStatus.add(CusConstant.STD_ZB_HAND_STATUS_30);
					inVO.setNeedStatus(needStatus);
					num += cusHandoverAppDao.updateStatusBySernoCondition(inVO);
					num += cusHandoverAppDao.updateLogStatusBySernoCondition(inVO);
					logger.info(serno + "所有表的移交更新总数:" + updateNum);
					allNum += num;
				}
				logger.info("日终跑批移交接收成功的客户业务资料信息结束,更新的数据总数:" + allNum);
			} catch (Exception e) {
				logger.error("日终跑批移交出现异常:", e);
				throw e;
			}
		}

		return allNum;
	}

}
